/*
 * Copyright (c) 2014 - 2017. Company.Domain Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.activity.delivery;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.BaseActivity;
import cn.com.dhc.danlu.model.AccountPaymentType;
import cn.com.dhc.danlu.util.CommonConst;

/**
 * author: 刘立琦(<a href="mailto:liuliqi@danlu.com">liuliqi@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-01-13 10:32<br/>
 *
 * <p>
 * 支付方式页面
 * “提交订单”页面--> 支付方式
 * </p>
 */
public class OrderPaymentWayActivity extends BaseActivity implements OnClickListener {
    /**
     * 四个选择图片
     */
    private ImageView onlinePay;
    private ImageView arrivePay;
    private ImageView companyChange;
    private ImageView onlineOrArrive;
    /**
     * 四个选择方式布局
     */
    private RelativeLayout rlltOnlinePay;
    private RelativeLayout rlltArrivePay;
    private RelativeLayout rlltOnlineOrArrive;
    /**
     * 四组支付文字
     */
    private TextView txtOnlinePay;
    private TextView txtHalfOnline;
    private TextView txtPlus;
    private TextView txtHalfArrive;
    private TextView txtArrivePay;
    /**
     * 三组tips
     */
    private TextView tvOnlineTips;
    private TextView tvArriveoronlineTips;
    private TextView tvArriveTips;
    /**
     * 意图对象
     */
    private Intent intent = new Intent();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_payment_way);

        initViews();
        setViews();
    }

    private void initViews() {
        onlinePay = (ImageView) findViewById(R.id.onlinePay);
        arrivePay = (ImageView) findViewById(R.id.arrivePay);
        companyChange = (ImageView) findViewById(R.id.companyChange);
        onlineOrArrive = (ImageView) findViewById(R.id.onlineOrArrive);
        rlltOnlinePay = (RelativeLayout) findViewById(R.id.rlltOnlinePay);
        rlltArrivePay = (RelativeLayout) findViewById(R.id.rlltArrivePay);
        rlltOnlineOrArrive = (RelativeLayout) findViewById(R.id.rlltOnlineOrArrive);
        txtOnlinePay = (TextView) findViewById(R.id.txtOnlinePay);
        txtHalfOnline = (TextView) findViewById(R.id.txtHalfOnline);
        txtPlus = (TextView) findViewById(R.id.txtPlus);
        txtHalfArrive = (TextView) findViewById(R.id.txtHalfArrive);
        txtArrivePay = (TextView) findViewById(R.id.txtArrivePay);
        tvOnlineTips = (TextView) findViewById(R.id.tv_online_tips);
        tvArriveoronlineTips = (TextView) findViewById(R.id.tv_arriveoronline_tips);
        tvArriveTips = (TextView) findViewById(R.id.tv_arrive_tips);
    }

    @Override
    protected void back() {
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected String getActivityTitle() {
        return null;
    }

    // 初始化画面
    private void setViews() {
        companyChange.setVisibility(View.GONE);

        Intent intent = getIntent();
        // 支付方式计算模型
        AccountPaymentType accountPaymentType = (AccountPaymentType) intent.getSerializableExtra("accountPaymentType");
        // 支付方式是否选中
        String paymentChecked = intent.getStringExtra("paymentChecked");

        onlinePay.setImageResource(R.drawable.ic_radio_button_uncecked);
        onlineOrArrive.setImageResource(R.drawable.ic_radio_button_uncecked);
        arrivePay.setImageResource(R.drawable.ic_radio_button_uncecked);

        if (accountPaymentType.isSupportCodForAllSeller()) {
            // 货到付款可选
            arrivePay.setVisibility(View.VISIBLE);
            txtArrivePay.setTextColor(this.getResources().getColor(R.color.can_choose_txt_color));
            tvArriveTips.setVisibility(View.GONE);
            rlltArrivePay.setOnClickListener(this);
        } else {
            tvArriveTips.setVisibility(View.VISIBLE);
            arrivePay.setVisibility(View.GONE);
            txtArrivePay.setTextColor(this.getResources().getColor(R.color.no_choose_txt_color));
        }
        if (!accountPaymentType.isSupportCodForAllSeller() && accountPaymentType.getSupportCodCount() > 0 && accountPaymentType.getOnLinePaymentCount() > 0) {
            // 货到+在线可选
            onlineOrArrive.setVisibility(View.VISIBLE);
            txtHalfOnline.setTextColor(this.getResources().getColor(R.color.can_choose_txt_color));
            txtPlus.setTextColor(this.getResources().getColor(R.color.can_choose_txt_color));
            txtHalfArrive.setTextColor(this.getResources().getColor(R.color.can_choose_txt_color));
            tvArriveoronlineTips.setVisibility(View.GONE);
            rlltOnlineOrArrive.setOnClickListener(this);
        } else {
            // 特殊情况
            rlltOnlineOrArrive.setVisibility(View.GONE);
            onlineOrArrive.setVisibility(View.GONE);
            tvArriveoronlineTips.setVisibility(View.VISIBLE);
            txtHalfOnline.setTextColor(this.getResources().getColor(R.color.no_choose_txt_color));
            txtPlus.setTextColor(this.getResources().getColor(R.color.no_choose_txt_color));
            txtHalfArrive.setTextColor(this.getResources().getColor(R.color.no_choose_txt_color));
        }

        if (accountPaymentType.isOnlinePayForAllSeller()) {
            // 在线支付可选
            onlinePay.setVisibility(View.VISIBLE);
            txtOnlinePay.setTextColor(this.getResources().getColor(R.color.can_choose_txt_color));
            tvOnlineTips.setVisibility(View.GONE);
            rlltOnlinePay.setOnClickListener(this);
        } else {
            tvOnlineTips.setVisibility(View.VISIBLE);
            onlinePay.setVisibility(View.GONE);
            txtOnlinePay.setTextColor(this.getResources().getColor(R.color.no_choose_txt_color));
        }
        if (!TextUtils.isEmpty(paymentChecked)) {
            // 选中默认支付方式
            if (this.getResources().getString(R.string.pay_online).equals(paymentChecked)) {
                onlinePay.setImageResource(R.drawable.checkbox_select);
            } else if (this.getResources().getString(R.string.pay_online_arrive).equals(paymentChecked)) {
                onlineOrArrive.setImageResource(R.drawable.checkbox_select);
            } else if (this.getResources().getString(R.string.pay_arrive).equals(paymentChecked)) {
                arrivePay.setImageResource(R.drawable.checkbox_select);
            }
        }
    }

    @Override
    public void onClick(View arg0) {
        selectTabLayout(arg0.getId());
    }

    /**
     * 支付方式
     */
    private void selectTabLayout(int viewId) {
        // 在线支付
        if (R.id.rlltOnlinePay == viewId) {
            onlinePay.setImageResource(R.drawable.checkbox_select);
            intent.putExtra(CommonConst.IntentKey.PAY_ID, 1);

        } else {
            onlinePay.setImageResource(R.drawable.ic_radio_button_uncecked);
        }

        // 货到付款+在线支付
        if (R.id.rlltOnlineOrArrive == viewId) {
            onlineOrArrive.setImageResource(R.drawable.checkbox_select);
            intent.putExtra(CommonConst.IntentKey.PAY_ID, 2);
        } else {
            onlineOrArrive.setImageResource(R.drawable.ic_radio_button_uncecked);
        }

        // 货到付款
        if (R.id.rlltArrivePay == viewId) {
            arrivePay.setImageResource(R.drawable.checkbox_select);
            intent.putExtra(CommonConst.IntentKey.PAY_ID, 3);
        } else {
            arrivePay.setImageResource(R.drawable.ic_radio_button_uncecked);
        }

        back();
    }
}
